package sample.stormax.pres.news;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import java.util.ArrayList;

import butterknife.ButterKnife;
import sample.stormax.pres.news.adapters.NewsAdapter;
import sample.stormax.pres.news.beans.News;
import sample.stormax.pres.news.fragments.FavouriteFragment;
import sample.stormax.pres.news.fragments.NewsContainerFragment;
import sample.stormax.pres.news.fragments.SettingFragment;
import sample.stormax.pres.news.utils.sharepres.SPUtils;

public class MainActivity extends AppCompatActivity {

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private Fragment favouriteFragment;
    private Fragment newsContainerFragment;
    private Fragment settingFragment;

    private static NewsAdapter favouriteAdapter;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment = null;
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    fragment = newsContainerFragment;
                    break;
                case R.id.navigation_favourite:
                    fragment = favouriteFragment;
                    break;
                case R.id.navigation_setting:
                    fragment = settingFragment;
                    break;
            }
            if (fragment != null) {
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();
                return true;
            } else return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        MyApplication application = (MyApplication) getApplication();

        //读取、写入SharePreference工具类
        SPUtils spUtils = new SPUtils(this);

        //获取GuideActivity中传来的数据
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        ArrayList<String> types;
        if (bundle != null) {
            types = bundle.getStringArrayList("TYPE");
            spUtils.addTypes(types);
        } else {
            types = spUtils.getTypes();
        }

        //中文tab
        ArrayList<String> typesCN = new ArrayList<>();
        typesCN.addAll(types);
        typesCN.set(0,"热点");

        //types键值
        for (int i = 1; i < types.size(); i ++) {
            types.set(i, application.typesTable.get(types.get(i)));
        }

        //设置默认fragment
        initFragment(types,typesCN);

        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }


    private void initFragment(ArrayList<String> types,ArrayList<String> typesCN) {
        //设置唯一的收藏夹适配器
        favouriteAdapter = NewsAdapter.getFavouriteAdapter(this);

        //获取fragmentManager
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();

        //实例化新闻容器fragment
        newsContainerFragment = NewsContainerFragment.newInstance(types,typesCN);

        //实例化收藏夹fragment
        favouriteFragment = FavouriteFragment.newInstance();
        ((FavouriteFragment)favouriteFragment).setAdapter(favouriteAdapter);

        //实例化设置fragment
        settingFragment = SettingFragment.newInstance();

        //设置默认的fragment
        fragmentTransaction.replace(R.id.fragment_container, newsContainerFragment);
        fragmentTransaction.commit();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}